<template>
    <div class="app-container">
      <el-card>
        <!-- 筛选 -->
        <el-row>
          <el-col :span="18">
            <el-form label-width="80px" size="small" :inline="true">
              <el-form-item label="学科名称">
                <el-input @clear="listapi" v-model="params.subjectName" style="200px"></el-input>
              </el-form-item>
              <el-form-item style="text-align: right">
                <el-button @click="clearSub">清除</el-button>
                <el-button type="primary" @click="listapi">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <!-- 点击事件 -->
            <el-button
              @click="show = true"
              icon="el-icon-edit"
              type="success"
              size="small"
              >新增学科</el-button
            >
          </el-col>
        </el-row>
        <!-- 数据记录 -->
        <el-alert
          :title="`数据一共 ${total} 条`"
          style="margin-bottom: 15px"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- 放置其他组件... -->
        <!-- 表格标签 -->
        <el-table border="" :data="list" style="width: 100%">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="subjectName" label="学科名称">
          </el-table-column>
          <el-table-column prop="creatorID" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template v-slot="data">
              <span>{{ $formatDate(data.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="isFrontDisplay" label="前台是否显示">
                        <template slot-scope="scope">
              {{ scope.row.isFrontDisplay === 1 ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>
          <el-table-column prop="tags" label="标签"> </el-table-column>
          <el-table-column prop="totals" label="题目数量"> </el-table-column>
          <el-table-column label="操作" width="240">
            <template #default="{ row }">
              <el-button type="text" @click="$router.push(`directorys?id=$(row.id)&name=${row.subjectName}`)">学科分类</el-button>
              <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
              <el-button type="text" @click="subhx(row)">修改</el-button>
              <el-button type="text" @click="delRole(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 50px"
        >
          <el-pagination
            background
            layout="prev, pager, next, total, sizes, jumper"
            :total="total"
            :page-sizes="[5, 10, 20]"
            :page-size="params.pagesize"
            :current-page="params.page"
            @current-change="changePage"
            @size-change="changePage1"
          >
          </el-pagination>
        </el-row>
        <!-- </el-row> -->
      </el-card>
      <!-- 新增学科 -->
      <el-dialog width="30%"
      :title="formData.id ? '修改学科' : '新增学科'"
      :visible.sync="show"
      @close="close"
      >
        <el-form ref="xk" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
              v-model="formData.subjectName"
              prop="subjectName"
              placeholder="请输入学科名称"
              type="text"
            >
            </el-input>
          </el-form-item>
          <!-- 是否显示开关 -->
          <el-form-item label="是否显示">
            <el-switch
              v-model="formData.isFrontDisplay"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="show = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>

<script>
import { list, remove, add, update, detail } from '@/api/hmmm/subjects'

export default {
  // name: 'subjects',
  data () {
    return {
      value: false,
      show: false,
      // 弹层表单
      formData: {
        subjectName: '', // 学科名称
        isFrontDisplay: '1' // 是否显示
      },
      // 校验规则
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]

      },
      // 角色列表
      list: [],
      total: 0,
      params: {
        page: 1,
        pagesize: 10,
        id: ''
      }
    }
  },
  created () {
    this.listapi()
    this.subhx()
  },
  methods: {
    // 点击删除学科
    delRole (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 确定
          await remove(row)
          this.$message.success('删除成功')
          this.listapi()
        })
        .catch(() => {
          // 取消
        })
    },
    // 切页
    changePage (newpage) {
      console.log('当前页码', newpage)
      // 修改查询参数的页码
      this.params.page = newpage
      // // 重新查询列表
      this.listapi()
    },
    changePage1 (newpage1) {
      this.params.pagesize = newpage1
      this.listapi()
    },
    // 获取数据列表
    async listapi () {
      const { data } = await list(this.params)
      console.log(data)
      this.total = data.counts
      this.list = data.items
    },
    // 新增学科
    submit () {
      this.$refs.xk.validate(async (isOk) => {
        if (isOk) {
          if (this.formData.id) {
            await update(this.formData)
            this.$message.success('修改成功')
          } else {
            await add(this.formData)
            this.$message.success('新增成功')
            this.params.page = 1
          }
          // 校验通过
          this.listapi()
          this.show = false
          this.close()
        }
      })
    },
    // 点击修改回显数据
    async subhx (id) {
      const list = await detail(id)
      console.log(list)
      this.formData.id = list.data.id
      this.formData.subjectName = list.data.subjectName
      this.show = true
    },
    // 清除input框内容
    clearSub () {
      this.params.subjectName = ''
      this.listapi()
    },
    close () {
      this.show = false
      this.formData = {
        id: '',
        subjectName: '', // 学科名称
        isFrontDisplay: '1' // 是否显示
      }
    }
  }
}

</script>

<style scoped>

</style>
